######################
#Packages
######################
library(rio)
library(car)
library(tidyverse)
library(cowplot)
library(ggpubr)


#############
#Pew/CNN
#############

#Import Data
pew_neither <- import("./Data/probs_1.dta")  %>%
  mutate(outcome = "Neither Party")

pew_one <- import("./Data/probs_2.dta")  %>%
  mutate(outcome = "One Party")

pew_both <- import("./Data/probs_3.dta")  %>%
  mutate(outcome = "Both Parties")

##Combine
pew_comb <- rbind(pew_neither, pew_one, pew_both) %>%
  rename(probs = `_margin`, 
         lower = `_ci_lb`, 
         upper = `_ci_ub`, 
         term = `_term`) %>%
  mutate(term = factor(term, 
                       levels=c(1,2), 
                       labels=c("PID Extremity", 
                                "Ideological Extremity"))) %>%
  select(-c(`_deriv`, `_predict`, `_at`, 
            `_atopt`, `_pvalue`, `_se`, `_statistic`))

##Figures

#Ideological Extremity
f1 <- pew_comb %>%
  filter(term == "Ideological Extremity") %>%
  rename(ideolext = `_m2`) %>%
  mutate(ideolext = factor(ideolext, levels=c(1,2,3), 
                           labels=c("Moderate", "Libs/Cons", "Very Libs/Cons")), 
         outcome = factor(outcome, levels=c("Neither Party", "One Party", "Both Parties"))) %>%
  ggplot(aes(x=ideolext, y=probs, group = outcome, shape = outcome)) + 
  geom_pointrange(aes(ymin=lower, ymax=upper)) + 
  geom_line() + 
  theme_bw() + 
  ylim(0,1) + 
  labs(title = "Pew/CNN: Ideological Extremity", 
       y = NULL, 
       x = NULL, 
       group = "Response", 
       shape = "Response", 
       x = "Ideological Extremity") + 
  theme(legend.position = "bottom") + 
  ggpubr::theme_pubclean() + 
  scale_shape_manual(values=c(17, 1, 0)) 

#PID Extremity
f2 <- pew_comb %>%
  filter(term == "PID Extremity") %>%
  rename(pidext = `_m1`) %>%
  mutate(pidext = factor(pidext, levels=c(1,2,3), 
                         labels=c("Independent", "Leaning Partisan", "Partisan")), 
         outcome = factor(outcome, levels=c("Neither Party", "One Party", "Both Parties"))) %>%
  ggplot(aes(x=pidext, y=probs, group = outcome, shape = outcome)) + 
  geom_pointrange(aes(ymin=lower, ymax=upper)) + 
  geom_line() + 
  theme_bw() + 
  ylim(0,1) + 
  labs(title = "Pew/CNN: Party ID Extremity", 
       y = NULL, 
       x = NULL,
       group = "Response", 
       shape = "Response", 
       x = "PID Extremity") + 
  theme(legend.position = "bottom") + 
  ggpubr::theme_pubclean() + 
  scale_shape_manual(values=c(17, 1, 0)) 


#############
#Lucid
#############
###Ideological Extremity
f3 <- import("./Data/lucid_sym.dta") %>%
  rename(outcome = `_predict`, 
         probs = `_margin`, 
         se = `_se`, 
         lower = `_ci_lb`, 
         upper = `_ci_ub`, 
         ideolext = `_m1`) %>%
  mutate(ideolext = factor(ideolext, 
                           levels=c(1,2,3,4), 
                           labels=c("Moderate.", "2", "3", "Very Lib/Cons")), 
         outcome = factor(outcome, 
                          levels=c(1,2,3), 
                          labels=c("Neither Party", 
                                   "One Party", 
                                   "Both Parties"))) %>%
  ggplot(aes(x=ideolext, y=probs, group=outcome, line=outcome, shape=outcome)) + 
  geom_pointrange(aes(ymin=lower, ymax=upper)) + 
  geom_line() + 
  theme_bw() + 
  labs(title = "Lucid: Ideological Extremity", 
       x = NULL,
       y = NULL, 
       shape = "Response", 
       line = "Response") + 
  scale_y_continuous(limits=c(0,1)) + 
  ggpubr::theme_pubclean() + 
  scale_shape_manual(values=c(17, 1, 0)) 

####Operational Ideology
f4 <-import("./Data/lucid_op.dta") %>%
  rename(outcome = `_predict`, 
         probs = `_margin`, 
         se = `_se`, 
         lower = `_ci_lb`, 
         upper = `_ci_ub`, 
         opideolcons = `_at3`) %>%
  mutate(outcome = factor(outcome, 
                          levels=c(1,2,3), 
                          labels=c("Neither Party", 
                                   "One Party", 
                                   "Both Parties"))) %>%
  ggplot(aes(x=opideolcons, y=probs, group=outcome, line=outcome, shape=outcome)) + 
  geom_pointrange(aes(ymin=lower, ymax=upper)) + 
  geom_line() + 
  theme_bw() + 
  theme(legend.position = "bottom") + 
  labs(title = "Lucid: Issue Consistency", 
       x = NULL,
       y = NULL, 
       shape = "Response", 
       line = "Response") + 
  scale_y_continuous(limits=c(0,1)) + 
  ggpubr::theme_pubclean() + 
  scale_shape_manual(values=c(17, 1, 0)) 


####PPD
f5 <-import("./Data/lucid_ppd.dta") %>% 
  rename(outcome = `_predict`, 
         probs = `_margin`, 
         se = `_se`, 
         lower = `_ci_lb`, 
         upper = `_ci_ub`, 
         ppd = `_at4`) %>%
  mutate(outcome = factor(outcome, 
                          levels=c(1,2,3), 
                          labels=c("Neither Party", 
                                   "One Party", 
                                   "Both Parties"))) %>%
  ggplot(aes(x=ppd, y=probs, group=outcome, line=outcome, shape=outcome)) + 
  geom_pointrange(aes(ymin=lower, ymax=upper)) + 
  geom_line() + 
  theme_bw() + 
  theme(legend.position = "bottom") + 
  labs(title = "Lucid: Perceived Policy Distance", 
       x = NULL,
       y = NULL, 
       shape = "Response", 
       line = "Response") + 
  scale_y_continuous(limits=c(0,1)) + 
  ggpubr::theme_pubclean() +
  scale_shape_manual(values=c(17, 1, 0)) 

###PID
f6 <-import("./Data/lucid_pid.dta") %>%
  rename(outcome = `_predict`, 
         probs = `_margin`, 
         se = `_se`, 
         lower = `_ci_lb`, 
         upper = `_ci_ub`, 
         pididstr = `_at1`) %>%
  mutate(outcome = factor(outcome, 
                          levels=c(1,2,3), 
                          labels=c("Neither Party", 
                                   "One Party", 
                                   "Both Parties"))) %>%
  ggplot(aes(x=pididstr, y=probs, group=outcome, line=outcome, shape=outcome)) + 
  geom_pointrange(aes(ymin=lower, ymax=upper)) + 
  geom_line() + 
  theme_bw() + 
  theme(legend.position = "bottom") + 
  labs(title = "Lucid: Party Identity Strength", 
       x = NULL,
       y = NULL, 
       shape = "Response", 
       line = "Response") + 
  scale_y_continuous(limits=c(0,1)) + 
  scale_x_continuous(breaks=c(1,2,3,4), 
                     labels=c("Pure Ind.", "2", "3", "Strongest\nPartisan")) + 
  ggpubr::theme_pubclean() + 
  scale_shape_manual(values=c(17, 1, 0)) 

#############
#Combined Figure
#############
ggpubr::ggarrange(f1, f3, f4, f5, f2, f6, 
                  common.legend=T, legend = "top", 
                  labels=c("A", "B", "C", "D", "E", "F"))


ggsave("figure3.png", 
       height=8, width=14, dpi=1200)
